﻿// ===============================================================================
// Copyright(c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:马星俊
// ===============================================================================

using System.Collections.Generic;
using System.Threading.Tasks;
using XCI.Sys.Model;
using XCI.Core;
using XCI.Sys.Filter;
using XCI.Windows;

namespace XCI.Sys.Service
{
    /// <summary>
    /// 系统角色服务
    /// </summary>
    public class SysRoleService
    {
        private const string RootUrl = "/api/sys/role";
        public static SysRoleService Instance { get; } = new SysRoleService();

        /// <summary>
        /// 新建角色模型
        /// </summary>
        public async Task<SysRole> NewAsync()
        {
            return await Task.FromResult(new SysRole
            {
                Status = true,
                DeptScope = 4,
            });
        }

        /// <summary>
        /// 复制角色模型
        /// </summary>
        /// <param name="id">原模型id</param>
        public async Task CopyAsync(string id)
        {
            var model = await SelectByIdAsync(id);
            model.Id = null;
            model.Name += " - 复制";
            model.Code += " - 复制";
            await SaveAsync(true,model);
        }

        /// <summary>
        /// 保存角色
        /// </summary>
        /// <param name="created">是否新增</param>
        /// <param name="model">角色模型对象</param>
        public async Task<BoolResult> SaveAsync(bool created, SysRole model)
        {
           return await App.Client.PostAsBoolResultAsync($"{RootUrl}/{(created ? "insert" : "update")}", model);
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <returns>成功返回true</returns>
        public async Task DeleteAsync(string ids)
        {
            await App.Client.PostAsync($"{RootUrl}/delete", new { ids });
        }

        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="ids">主键字符串</param>
        /// <param name="status">状态,1启用;0禁用</param>
        public async Task UpdateStatusAsync(string ids, bool status)
        {
            await App.Client.PostAsync($"{RootUrl}/updateStatus", new { ids, status });
        }

        /// <summary>
        /// 根据主键获取角色对象
        /// </summary>
        /// <param name="id">角色主键</param>
        public async Task<SysRole> SelectByIdAsync(string id)
        {
            return await App.Client.PostAsync<SysRole>($"{RootUrl}/selectById", new { id });
        }

        /// <summary>
        /// 根据主键获取角色对象
        /// </summary>
        /// <param name="args">编辑参数</param>
        public Task<SysRole> GetOrCreateAsync(EditFormArgs args)
        {
            if (args == null || args.Created) return NewAsync();
            return SelectByIdAsync(args.Id);
        }

        /// <summary>
        /// 查询角色分页列表
        /// </summary>
        /// <param name="filter">过滤条件</param>
        public async Task<PagingList<SysRole>> SelectPageListAsync(SysRoleFilter filter)
        {
            return await App.Client.PostAsync<PagingList<SysRole>>($"{RootUrl}/selectPageList", filter);
        }

        /// <summary>
        /// 查询角色列表
        /// </summary>
        /// <param name="filter">过滤对象</param>
        public async Task<List<SysRole>> SelectListAsync(SysRoleFilter filter)
        {
            return await App.Client.PostAsync<List<SysRole>>($"{RootUrl}/selectList", filter);
        }

        /// <summary>
        /// 导出角色列表
        /// </summary>
        /// <param name="filePath">保存文件路径</param>
        /// <param name="filter">过滤对象</param>
        public async Task ExportAsync(string filePath, SysRoleFilter filter)
        {
            await App.Client.DownloadAsync($"{RootUrl}/export", filePath, filter);
        }

        /// <summary>
        /// 刷新服务端角色缓存
        /// </summary>
        public async Task RefreshCacheAsync()
        {
            await App.Client.PostAsync($"{RootUrl}/refreshCache");
        }

        /// <summary>
        /// 保存角色权限
        /// </summary>
        /// <param name="permissionBody">角色权限数据</param>
        public async Task SaveRolePermissionAsync(RolePermissionBody permissionBody)
        {
            await App.Client.PostAsync($"{RootUrl}/saveRolePermission", permissionBody);
        }

        /// <summary>
        /// 清除角色权限
        /// </summary>
        public async Task ClearRolePermissionAsync(string roleId)
        {
            await App.Client.PostAsync($"{RootUrl}/clearRolePermission", new { roleId });
        }

        /// <summary>
        /// 查询角色权限
        /// </summary>
        /// <param name="roleId">角色主键</param>
        public async Task<SysRolePermission> SelectRolePermissionAsync(string roleId)
        {
            return await App.Client.PostAsync<SysRolePermission>($"{RootUrl}/selectRolePermission", new { roleId });
        }

        /// <summary>
        /// 根据用户主键查询关系角色对象列表
        /// </summary>
        public async Task<List<SysRole>> SelectRelationRoleListAsync(string userId)
        {
            return await App.Client.PostAsync<List<SysRole>>($"{RootUrl}/selectRelationRoleList", new { userId });
        }
    }
}
